//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <stdlib.h>
#include "Unit1.h"
#include "about.h"
#include "caution.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

#define EMPTY 0
#define WHITE 1
#define RED 2
#define NIL 999

//===========================================================================
//                                               DEKLARASI KELAS DAN VARIABEL
//===========================================================================
int id=NIL;
int _x=NIL;
int _y=NIL;

int xAwal;
int xAkhir;
int yAwal;
int yAkhir;
int longRect;

int row, col;
int turn;
int brdSize = 10;
bool isKing = false;
bool anotherChance = false;
bool isPlay = false;

int rowColToPixel[10];

class clsBoard {                                  
   public:
   int piece;
   bool isKing;
} board[10][10];

int pixelToRowCol(int pixel) {
	int hasil;
	hasil = (pixel-xAwal+longRect)/longRect;
	return hasil;
}

//===========================================================================
// fungsi dan prosedur
//===========================================================================
bool isWhite(int id) {
	if (id/100 == 1) {
		return true;
	} else {
		return false;
	}
}

bool isRed(int id) {
	if (id/100 == 2) {
		return true;
	} else {
		return false;
	}
}

bool diagonal(int X1, int Y1, int X2, int Y2, int id) {	// gerakin piece 1 diagonal
	// X1,Y1 = titik baru; X2,Y2 = titik lama
	int x1,y1,x2,y2,dx,dy;
	
	if (X1 == NIL || Y1 == NIL || X2 == NIL || Y2 == NIL) {
		return false;
	}
	
	x1 = (X1 - xAwal) / longRect;
	y1 = (Y1 - yAwal) / longRect;
	x2 = (X2 - xAwal) / longRect;
	y2 = (Y2 - yAwal) / longRect;
	dx = x1 - x2;
	dy = y1 - y2;
	
	if ((dx==1 || dx==-1) && (dy==1 || dy==-1) && isKing) {
		return true;
	} else if ((dx==1 || dx==-1) && dy==1 && isWhite(id)) {
		return true;
	} else if ((dx==1 || dx==-1) && dy==-1 && isRed(id)) {
		return true;
	} else {
		return false;
	}
}

bool diagonal2(int X1, int Y1, int X2, int Y2, clsBoard board[10][10]) { // gerakin piece 2 diagonal (sambil makan lawan)
	// X1,Y1 = titik baru; X2,Y2 = titik lama
	int x1,y1,x2,y2,dx,dy;
	int xTengah,yTengah;
	
	if (X1 == NIL || Y1 == NIL || X2 == NIL || Y2 == NIL) {
		return false;
	}
	
	x1 = (X1 - xAwal) / longRect;
	y1 = (Y1 - yAwal) / longRect;
	x2 = (X2 - xAwal) / longRect;
	y2 = (Y2 - yAwal) / longRect;
	dx = x1 - x2;
	dy = y1 - y2;
	xTengah = (X1+X2)/2;
	yTengah = (Y1+Y2)/2;
	
	if ((dx==2 || dx==-2) && (dy==2 || dy==-2)) {
		if (isRed(board[(X2-xAwal)/longRect][(Y2-yAwal)/longRect].piece) && isWhite(board[(xTengah-xAwal)/longRect][(yTengah-yAwal)/longRect].piece) && (dy==-2 || isKing)) {
			return true;
		} else if (isWhite(board[(X2-xAwal)/longRect][(Y2-yAwal)/longRect].piece) && isRed(board[(xTengah-xAwal)/longRect][(yTengah-yAwal)/longRect].piece) && (dy==2 || isKing)) {
			return true;
		}
		return false;
	} else {
		return false;
	}
}

bool cekKiriDepan(clsBoard board[10][10], int row, int col) {
	bool status = isRed(board[row][col].piece);
	if (status!=isRed(board[row-1][col-1].piece) && board[row-1][col-1].piece!=EMPTY && (board[row-2][col-2].piece==EMPTY)) {
		return true;
	} else {
		return false;
	}
}

bool stuckKiriDepan(clsBoard board[10][10], int row, int col) {
	if (row==0 || col==0) { // paling kiri atau paling depan
		return true;
	} else if ((row==1 || col==1) && board[row-1][col-1].piece!=EMPTY) { // 1 kolom dari kiri atau depan
		return true;
	} else if (board[row-1][col-1].piece!=EMPTY && board[row-2][col-2].piece!=EMPTY) {
		return true;
	} else {return false;}
}

bool cekKananDepan(clsBoard board[10][10], int row, int col) {
	bool status = isRed(board[row][col].piece);
	if (status!=isRed(board[row+1][col-1].piece) && board[row+1][col-1].piece!=EMPTY && (board[row+2][col-2].piece==EMPTY)) {
		return true;
	} else {
		return false;
	}
}

bool stuckKananDepan(clsBoard board[10][10], int row, int col) {
	if (row==(brdSize-1) || col==0) { // paling kanan atau paling depan
		return true;
	} else if ((row==(brdSize-2) || col==1) && board[row+1][col-1].piece!=EMPTY) { // 1 kolom dari kanan atau depan
		return true;
	} else if (board[row+1][col-1].piece!=EMPTY && board[row+2][col-2].piece!=EMPTY) {
		return true;
	} else {return false;}
}

bool cekKiriBelakang(clsBoard board[10][10], int row, int col) {
	bool status = isRed(board[row][col].piece);
	if (status!=isRed(board[row-1][col+1].piece) && board[row-1][col+1].piece!=EMPTY && (board[row-2][col+2].piece==EMPTY)) {
		return true;
	} else {
		return false;
	}
}

bool stuckKiriBelakang(clsBoard board[10][10], int row, int col) {
	if (row==0 || col==(brdSize-1)) { // paling kiri atau paling belakang
		return true;
	} else if ((row==1 || col==(brdSize-2)) && board[row-1][col+1].piece!=EMPTY) { // 1 kolom dari kiri atau belakang
		return true;
	} else if (board[row-1][col+1].piece!=EMPTY && board[row-2][col+2].piece!=EMPTY) {
		return true;
	} else {return false;}
}

bool cekKananBelakang(clsBoard board[10][10], int row, int col) {
	bool status = isRed(board[row][col].piece);
	if (status!=isRed(board[row+1][col+1].piece) && board[row+1][col+1].piece!=EMPTY && (board[row+2][col+2].piece==EMPTY)) {
		return true;
	} else {
		return false;
	}
}

bool stuckKananBelakang(clsBoard board[10][10], int row, int col) {
	if (row==(brdSize-1) || col==(brdSize-1)) { // paling kanan atau paling belakang
		return true;
	} else if ((row==6 || col==6) && board[row+1][col+1].piece!=EMPTY) { // 1 kolom dari kanan atau belakang
		return true;
	} else if (board[row+1][col+1].piece!=EMPTY && board[row+2][col+2].piece!=EMPTY) {
		return true;
	} else {return false;}
}

bool cekDepan(clsBoard board[10][10], int row, int col) {
	if (col<=1) { // terlalu kedepan
		return false; 
	} else if (row<=1) { // terlalu ke kiri
		return cekKananDepan(board,row,col);
	} else if (row>=(brdSize-2)) { // terlalu ke kanan
		return cekKiriDepan(board,row,col);
	} else { // bisa ke segala arah
		return (cekKananDepan(board,row,col) || cekKiriDepan(board,row,col));
	}
}

bool stuckDepan(clsBoard board[10][10], int row, int col) {
	return (stuckKiriDepan(board,row,col) && stuckKananDepan(board,row,col));
}

bool cekBelakang(clsBoard board[10][10], int row, int col) {
	if (col>=(brdSize-2)) { // terlalu kebelakang
		return false; 
	} else if (row<=1) { // terlalu ke kiri
		return cekKananBelakang(board,row,col);
	} else if (row>=(brdSize-2)) { // terlalu ke kanan
		return cekKiriBelakang(board,row,col);
	} else { // bisa ke segala arah
		return (cekKananBelakang(board,row,col) || cekKiriBelakang(board,row,col));
	}
}

bool stuckBelakang(clsBoard board[10][10], int row, int col) {
	return (stuckKiriBelakang(board,row,col) && stuckKananBelakang(board,row,col));
}

bool isChance(int turn, clsBoard board[10][10]) { // apakah ada checker yang bisa di bunuh
	int row, col;
	bool status=false;
	
	if (turn == WHITE) {
		for (row = 0; row < brdSize; row++) {
			for (col = 0; col < brdSize; col++) {
				if (isWhite(board[row][col].piece)) { // ketemu white
					if (board[row][col].isKing) { // ketemu king white
						status = cekDepan(board,row,col) || cekBelakang(board,row,col);
					} else { // white biasa
						status = cekBelakang(board,row,col);
					}
					if (status) {
						return true;
					}
				}
			}
		}
	} else if (turn == RED) {
		for (row = 0; row < brdSize; row++) {
			for (col = 0; col < brdSize; col++) {
				if (isRed(board[row][col].piece)) { // ketemu red
					if (board[row][col].isKing) { // ketemu king red
						status = cekDepan(board,row,col) || cekBelakang(board,row,col);
					} else { // red biasa
						status = cekDepan(board,row,col);
					}
					if (status) {
						return true;
					}
				}
			}
		}		
	}
	return false;
}

bool isChancePiece(clsBoard board[10][10], int row, int col) {
	if (board[row][col].isKing) {
		return (cekDepan(board,row,col) || cekBelakang(board,row,col));
	} else if (isWhite(board[row][col].piece)) {
		return cekBelakang(board,row,col);
	} // if (isRed(board[row][col].piece))
	return cekDepan(board,row,col);
}

bool isStuck(int turn, clsBoard board[10][10]) { // apakah piece turn sudah stuck
	int row, col;
	bool status=false;
	
	if (turn == WHITE) {
		for (row = 0; row < brdSize; row++) {
			for (col = 0; col < brdSize; col++) {
				if (isWhite(board[row][col].piece)) { // ketemu white
					if (board[row][col].isKing) { // ketemu king white
						status = stuckDepan(board,row,col) && stuckBelakang(board,row,col);
					} else { // white biasa
						status = stuckBelakang(board,row,col);
					}
					if (!status) {
						return false;
					}
				}
			}
		}
	} else if (turn == RED) {
		for (row = 0; row < brdSize; row++) {
			for (col = 0; col < brdSize; col++) {
				if (isRed(board[row][col].piece)) { // ketemu red
					if (board[row][col].isKing) { // ketemu king red
						status = stuckDepan(board,row,col) && stuckBelakang(board,row,col);
					} else { // red biasa
						status = stuckDepan(board,row,col);
					}
					if (!status) {
						return false;
					}
				}
			}
		}		
	}
	return true;
}

void killPiece(int id) {
	switch (id) {
		// kill white
		case (101) : {Form1->whi01->Visible = false; break;}
		case (102) : {Form1->whi02->Visible = false; break;}
		case (103) : {Form1->whi03->Visible = false; break;}
		case (104) : {Form1->whi04->Visible = false; break;}
		case (105) : {Form1->whi05->Visible = false; break;}
		case (106) : {Form1->whi06->Visible = false; break;}
		case (107) : {Form1->whi07->Visible = false; break;}
		case (108) : {Form1->whi08->Visible = false; break;}
		case (109) : {Form1->whi09->Visible = false; break;}
		case (110) : {Form1->whi10->Visible = false; break;}
		case (111) : {Form1->whi11->Visible = false; break;}
		case (112) : {Form1->whi12->Visible = false; break;}
		case (113) : {Form1->whi13->Visible = false; break;}
		case (114) : {Form1->whi14->Visible = false; break;}
		case (115) : {Form1->whi15->Visible = false; break;}
		// kill red
		case (201) : {Form1->red01->Visible = false; break;}
		case (202) : {Form1->red02->Visible = false; break;}
		case (203) : {Form1->red03->Visible = false; break;}
		case (204) : {Form1->red04->Visible = false; break;}
		case (205) : {Form1->red05->Visible = false; break;}
		case (206) : {Form1->red06->Visible = false; break;}
		case (207) : {Form1->red07->Visible = false; break;}
		case (208) : {Form1->red08->Visible = false; break;}
		case (209) : {Form1->red09->Visible = false; break;}
		case (210) : {Form1->red10->Visible = false; break;}
		case (211) : {Form1->red11->Visible = false; break;}
		case (212) : {Form1->red12->Visible = false; break;}
		case (213) : {Form1->red13->Visible = false; break;}
		case (214) : {Form1->red14->Visible = false; break;}
		case (215) : {Form1->red15->Visible = false; break;}
	}
}

void showKing(int id) {
	if (brdSize == 8) {
		switch (id) {
			// white
			case (101) : {Form1->whi01->Picture = Form1->kingWhite->Picture; break;}
			case (102) : {Form1->whi02->Picture = Form1->kingWhite->Picture; break;}
			case (103) : {Form1->whi03->Picture = Form1->kingWhite->Picture; break;}
			case (104) : {Form1->whi04->Picture = Form1->kingWhite->Picture; break;}
			case (105) : {Form1->whi05->Picture = Form1->kingWhite->Picture; break;}
			case (106) : {Form1->whi06->Picture = Form1->kingWhite->Picture; break;}
			case (107) : {Form1->whi07->Picture = Form1->kingWhite->Picture; break;}
			case (108) : {Form1->whi08->Picture = Form1->kingWhite->Picture; break;}
			case (109) : {Form1->whi09->Picture = Form1->kingWhite->Picture; break;}
			case (110) : {Form1->whi10->Picture = Form1->kingWhite->Picture; break;}
			case (111) : {Form1->whi11->Picture = Form1->kingWhite->Picture; break;}
			case (112) : {Form1->whi12->Picture = Form1->kingWhite->Picture; break;}
			case (113) : {Form1->whi13->Picture = Form1->kingWhite->Picture; break;}
			case (114) : {Form1->whi14->Picture = Form1->kingWhite->Picture; break;}
			case (115) : {Form1->whi15->Picture = Form1->kingWhite->Picture; break;}
			// red
			case (201) : {Form1->red01->Picture = Form1->kingRed->Picture; break;}
			case (202) : {Form1->red02->Picture = Form1->kingRed->Picture; break;}
			case (203) : {Form1->red03->Picture = Form1->kingRed->Picture; break;}
			case (204) : {Form1->red04->Picture = Form1->kingRed->Picture; break;}
			case (205) : {Form1->red05->Picture = Form1->kingRed->Picture; break;}
			case (206) : {Form1->red06->Picture = Form1->kingRed->Picture; break;}
			case (207) : {Form1->red07->Picture = Form1->kingRed->Picture; break;}
			case (208) : {Form1->red08->Picture = Form1->kingRed->Picture; break;}
			case (209) : {Form1->red09->Picture = Form1->kingRed->Picture; break;}
			case (210) : {Form1->red10->Picture = Form1->kingRed->Picture; break;}
			case (211) : {Form1->red11->Picture = Form1->kingRed->Picture; break;}
			case (212) : {Form1->red12->Picture = Form1->kingRed->Picture; break;}
			case (213) : {Form1->red13->Picture = Form1->kingRed->Picture; break;}
			case (214) : {Form1->red14->Picture = Form1->kingRed->Picture; break;}
			case (215) : {Form1->red15->Picture = Form1->kingRed->Picture; break;}
		}
	} else if (brdSize == 10) {
		switch (id) {
			// white
			case (101) : {Form1->whi01->Picture = Form1->smallKingWhite->Picture; break;}
			case (102) : {Form1->whi02->Picture = Form1->smallKingWhite->Picture; break;}
			case (103) : {Form1->whi03->Picture = Form1->smallKingWhite->Picture; break;}
			case (104) : {Form1->whi04->Picture = Form1->smallKingWhite->Picture; break;}
			case (105) : {Form1->whi05->Picture = Form1->smallKingWhite->Picture; break;}
			case (106) : {Form1->whi06->Picture = Form1->smallKingWhite->Picture; break;}
			case (107) : {Form1->whi07->Picture = Form1->smallKingWhite->Picture; break;}
			case (108) : {Form1->whi08->Picture = Form1->smallKingWhite->Picture; break;}
			case (109) : {Form1->whi09->Picture = Form1->smallKingWhite->Picture; break;}
			case (110) : {Form1->whi10->Picture = Form1->smallKingWhite->Picture; break;}
			case (111) : {Form1->whi11->Picture = Form1->smallKingWhite->Picture; break;}
			case (112) : {Form1->whi12->Picture = Form1->smallKingWhite->Picture; break;}
			case (113) : {Form1->whi13->Picture = Form1->smallKingWhite->Picture; break;}
			case (114) : {Form1->whi14->Picture = Form1->smallKingWhite->Picture; break;}
			case (115) : {Form1->whi15->Picture = Form1->smallKingWhite->Picture; break;}
			// red
			case (201) : {Form1->red01->Picture = Form1->smallKingRed->Picture; break;}
			case (202) : {Form1->red02->Picture = Form1->smallKingRed->Picture; break;}
			case (203) : {Form1->red03->Picture = Form1->smallKingRed->Picture; break;}
			case (204) : {Form1->red04->Picture = Form1->smallKingRed->Picture; break;}
			case (205) : {Form1->red05->Picture = Form1->smallKingRed->Picture; break;}
			case (206) : {Form1->red06->Picture = Form1->smallKingRed->Picture; break;}
			case (207) : {Form1->red07->Picture = Form1->smallKingRed->Picture; break;}
			case (208) : {Form1->red08->Picture = Form1->smallKingRed->Picture; break;}
			case (209) : {Form1->red09->Picture = Form1->smallKingRed->Picture; break;}
			case (210) : {Form1->red10->Picture = Form1->smallKingRed->Picture; break;}
			case (211) : {Form1->red11->Picture = Form1->smallKingRed->Picture; break;}
			case (212) : {Form1->red12->Picture = Form1->smallKingRed->Picture; break;}
			case (213) : {Form1->red13->Picture = Form1->smallKingRed->Picture; break;}
			case (214) : {Form1->red14->Picture = Form1->smallKingRed->Picture; break;}
			case (215) : {Form1->red15->Picture = Form1->smallKingRed->Picture; break;}		
		}
	}
}

void sizeDefault(int brdSize) {
	if (brdSize == 8) {
		xAwal = 44; xAkhir = 556; yAwal = 44; yAkhir = 556; longRect = 64;
	} else if (brdSize == 10) {
		xAwal = 50; xAkhir = 550; yAwal = 50; yAkhir = 550; longRect = 50;
	}
	Form1->whi01->Height = longRect; Form1->whi01->Width = longRect;
	Form1->whi02->Height = longRect; Form1->whi02->Width = longRect;
	Form1->whi03->Height = longRect; Form1->whi03->Width = longRect;
	Form1->whi04->Height = longRect; Form1->whi04->Width = longRect;
	Form1->whi05->Height = longRect; Form1->whi05->Width = longRect;
	Form1->whi06->Height = longRect; Form1->whi06->Width = longRect;
	Form1->whi07->Height = longRect; Form1->whi07->Width = longRect;
	Form1->whi08->Height = longRect; Form1->whi08->Width = longRect;
	Form1->whi09->Height = longRect; Form1->whi09->Width = longRect;
	Form1->whi10->Height = longRect; Form1->whi10->Width = longRect;
	Form1->whi11->Height = longRect; Form1->whi11->Width = longRect;
	Form1->whi12->Height = longRect; Form1->whi12->Width = longRect;
	Form1->whi13->Height = longRect; Form1->whi13->Width = longRect;
	Form1->whi14->Height = longRect; Form1->whi14->Width = longRect;
	Form1->whi15->Height = longRect; Form1->whi15->Width = longRect;
	// konversi dari baris dan kolom ke pixel (sebaliknya)
	for (int i=0;i<=9;i++) {
		rowColToPixel[i] = xAwal+i*longRect;
	}
}

void pictureDefault(int brdSize) {
	if (brdSize == 8) {
		// white
		Form1->whi01->Picture = Form1->whi->Picture;Form1->whi02->Picture = Form1->whi->Picture;Form1->whi03->Picture = Form1->whi->Picture;
		Form1->whi04->Picture = Form1->whi->Picture;Form1->whi05->Picture = Form1->whi->Picture;Form1->whi06->Picture = Form1->whi->Picture;
		Form1->whi07->Picture = Form1->whi->Picture;Form1->whi08->Picture = Form1->whi->Picture;Form1->whi09->Picture = Form1->whi->Picture;
		Form1->whi10->Picture = Form1->whi->Picture;Form1->whi11->Picture = Form1->whi->Picture;Form1->whi12->Picture = Form1->whi->Picture;
		Form1->whi13->Picture = Form1->whi->Picture;Form1->whi14->Picture = Form1->whi->Picture;Form1->whi15->Picture = Form1->whi->Picture;
		// red
		Form1->red01->Picture = Form1->red->Picture;Form1->red02->Picture = Form1->red->Picture;Form1->red03->Picture = Form1->red->Picture;
		Form1->red04->Picture = Form1->red->Picture;Form1->red05->Picture = Form1->red->Picture;Form1->red06->Picture = Form1->red->Picture;
		Form1->red07->Picture = Form1->red->Picture;Form1->red08->Picture = Form1->red->Picture;Form1->red09->Picture = Form1->red->Picture;
		Form1->red10->Picture = Form1->red->Picture;Form1->red11->Picture = Form1->red->Picture;Form1->red12->Picture = Form1->red->Picture;
		Form1->red13->Picture = Form1->red->Picture;Form1->red14->Picture = Form1->red->Picture;Form1->red15->Picture = Form1->red->Picture;
		//board
		Form1->Board->Picture = Form1->Board8->Picture;
		//light
		Form1->light->Picture = Form1->light8->Picture;		
	} else if (brdSize == 10) {
		// white
		Form1->whi01->Picture = Form1->smallWhite->Picture;Form1->whi02->Picture = Form1->smallWhite->Picture;Form1->whi03->Picture = Form1->smallWhite->Picture;
		Form1->whi04->Picture = Form1->smallWhite->Picture;Form1->whi05->Picture = Form1->smallWhite->Picture;Form1->whi06->Picture = Form1->smallWhite->Picture;
		Form1->whi07->Picture = Form1->smallWhite->Picture;Form1->whi08->Picture = Form1->smallWhite->Picture;Form1->whi09->Picture = Form1->smallWhite->Picture;
		Form1->whi10->Picture = Form1->smallWhite->Picture;Form1->whi11->Picture = Form1->smallWhite->Picture;Form1->whi12->Picture = Form1->smallWhite->Picture;
		Form1->whi13->Picture = Form1->smallWhite->Picture;Form1->whi14->Picture = Form1->smallWhite->Picture;Form1->whi15->Picture = Form1->smallWhite->Picture;
		// red
		Form1->red01->Picture = Form1->smallRed->Picture;Form1->red02->Picture = Form1->smallRed->Picture;Form1->red03->Picture = Form1->smallRed->Picture;
		Form1->red04->Picture = Form1->smallRed->Picture;Form1->red05->Picture = Form1->smallRed->Picture;Form1->red06->Picture = Form1->smallRed->Picture;
		Form1->red07->Picture = Form1->smallRed->Picture;Form1->red08->Picture = Form1->smallRed->Picture;Form1->red09->Picture = Form1->smallRed->Picture;
		Form1->red10->Picture = Form1->smallRed->Picture;Form1->red11->Picture = Form1->smallRed->Picture;Form1->red12->Picture = Form1->smallRed->Picture;
		Form1->red13->Picture = Form1->smallRed->Picture;Form1->red14->Picture = Form1->smallRed->Picture;Form1->red15->Picture = Form1->smallRed->Picture;		
		//board
		Form1->Board->Picture = Form1->Board10->Picture;
		//light
		Form1->light->Picture = Form1->light10->Picture;
	}
}

void positionDefault(int brdSize) {
	// white
	Form1->whi01->Left=xAwal+longRect;Form1->whi01->Top=yAwal+2*longRect;Form1->whi01->Visible=true;
	Form1->whi02->Left=xAwal+3*longRect;Form1->whi02->Top=yAwal+2*longRect;Form1->whi02->Visible=true;
	Form1->whi03->Left=xAwal+5*longRect;Form1->whi03->Top=yAwal+2*longRect;Form1->whi03->Visible=true;
	Form1->whi04->Left=xAwal+7*longRect;Form1->whi04->Top=yAwal+2*longRect;Form1->whi04->Visible=true;
	Form1->whi05->Left=xAwal;Form1->whi05->Top=yAwal+longRect;Form1->whi05->Visible=true;
	Form1->whi06->Left=xAwal+2*longRect;Form1->whi06->Top=yAwal+longRect;Form1->whi06->Visible=true;
	Form1->whi07->Left=xAwal+4*longRect;Form1->whi07->Top=yAwal+longRect;Form1->whi07->Visible=true;
	Form1->whi08->Left=xAwal+6*longRect;Form1->whi08->Top=yAwal+longRect;Form1->whi08->Visible=true;
	Form1->whi09->Left=xAwal+longRect;Form1->whi09->Top=yAwal;Form1->whi09->Visible=true;
	Form1->whi10->Left=xAwal+3*longRect;Form1->whi10->Top=yAwal;Form1->whi10->Visible=true;
	Form1->whi11->Left=xAwal+5*longRect;Form1->whi11->Top=yAwal;Form1->whi11->Visible=true;
	Form1->whi12->Left=xAwal+7*longRect;Form1->whi12->Top=yAwal;Form1->whi12->Visible=true;
	Form1->whi13->Left=xAwal+9*longRect;Form1->whi13->Top=yAwal+2*longRect;
	Form1->whi14->Left=xAwal+8*longRect;Form1->whi14->Top=yAwal+longRect;
	Form1->whi15->Left=xAwal+9*longRect;Form1->whi15->Top=yAwal;
	
	if (brdSize == 8) {
		// red
		Form1->red01->Left=xAwal;Form1->red01->Top=yAwal+5*longRect;Form1->red01->Visible=true;
		Form1->red02->Left=xAwal+2*longRect;Form1->red02->Top=yAwal+5*longRect;Form1->red02->Visible=true;
		Form1->red03->Left=xAwal+4*longRect;Form1->red03->Top=yAwal+5*longRect;Form1->red03->Visible=true;
		Form1->red04->Left=xAwal+6*longRect;Form1->red04->Top=yAwal+5*longRect;Form1->red04->Visible=true;
		Form1->red05->Left=xAwal+longRect;Form1->red05->Top=yAwal+6*longRect;Form1->red05->Visible=true;
		Form1->red06->Left=xAwal+3*longRect;Form1->red06->Top=yAwal+6*longRect;Form1->red06->Visible=true;	
		Form1->red07->Left=xAwal+5*longRect;Form1->red07->Top=yAwal+6*longRect;Form1->red07->Visible=true;
		Form1->red08->Left=xAwal+7*longRect;Form1->red08->Top=yAwal+6*longRect;Form1->red08->Visible=true;	
		Form1->red09->Left=xAwal;Form1->red09->Top=yAwal+7*longRect;Form1->red09->Visible=true;
		Form1->red10->Left=xAwal+2*longRect;Form1->red10->Top=yAwal+7*longRect;Form1->red10->Visible=true;
		Form1->red11->Left=xAwal+4*longRect;Form1->red11->Top=yAwal+7*longRect;Form1->red11->Visible=true;
		Form1->red12->Left=xAwal+6*longRect;Form1->red12->Top=yAwal+7*longRect;Form1->red12->Visible=true;
		Form1->red13->Left=xAwal+8*longRect;Form1->red13->Top=yAwal+5*longRect;
		Form1->red14->Left=xAwal+9*longRect;Form1->red14->Top=yAwal+6*longRect;
		Form1->red15->Left=xAwal+8*longRect;Form1->red15->Top=yAwal+7*longRect;

		Form1->whi13->Visible=false; Form1->whi14->Visible=false; Form1->whi15->Visible=false;
		Form1->red13->Visible=false; Form1->red14->Visible=false; Form1->red15->Visible=false;
	} else if (brdSize == 10) {
		Form1->red01->Left=xAwal;Form1->red01->Top=yAwal+7*longRect;Form1->red01->Visible=true;
		Form1->red02->Left=xAwal+2*longRect;Form1->red02->Top=yAwal+7*longRect;Form1->red02->Visible=true;
		Form1->red03->Left=xAwal+4*longRect;Form1->red03->Top=yAwal+7*longRect;Form1->red03->Visible=true;
		Form1->red04->Left=xAwal+6*longRect;Form1->red04->Top=yAwal+7*longRect;Form1->red04->Visible=true;
		Form1->red05->Left=xAwal+longRect;Form1->red05->Top=yAwal+8*longRect;Form1->red05->Visible=true;
		Form1->red06->Left=xAwal+3*longRect;Form1->red06->Top=yAwal+8*longRect;Form1->red06->Visible=true;	
		Form1->red07->Left=xAwal+5*longRect;Form1->red07->Top=yAwal+8*longRect;Form1->red07->Visible=true;
		Form1->red08->Left=xAwal+7*longRect;Form1->red08->Top=yAwal+8*longRect;Form1->red08->Visible=true;	
		Form1->red09->Left=xAwal;Form1->red09->Top=yAwal+9*longRect;Form1->red09->Visible=true;
		Form1->red10->Left=xAwal+2*longRect;Form1->red10->Top=yAwal+9*longRect;Form1->red10->Visible=true;
		Form1->red11->Left=xAwal+4*longRect;Form1->red11->Top=yAwal+9*longRect;Form1->red11->Visible=true;
		Form1->red12->Left=xAwal+6*longRect;Form1->red12->Top=yAwal+9*longRect;Form1->red12->Visible=true;
		Form1->red13->Left=xAwal+8*longRect;Form1->red13->Top=yAwal+7*longRect;
		Form1->red14->Left=xAwal+9*longRect;Form1->red14->Top=yAwal+8*longRect;
		Form1->red15->Left=xAwal+8*longRect;Form1->red15->Top=yAwal+9*longRect;

		Form1->whi13->Visible=true; Form1->whi14->Visible=true; Form1->whi15->Visible=true;
		Form1->red13->Visible=true; Form1->red14->Visible=true; Form1->red15->Visible=true;		
	}
}

void idDefault(clsBoard board[10][10], int brdSize) {
	int row, col;
	// isi status board
    
	for (row = 0; row < 10; row++) {
		for (col = 0; col < 10; col++) {
			board[row][col].piece = EMPTY;
			board[row][col].isKing = false;
		}
    }
	
	if (brdSize == 8) {
		board[1][0].piece = 109;board[3][0].piece = 110;board[5][0].piece = 111;board[7][0].piece = 112;
		board[0][1].piece = 105;board[2][1].piece = 106;board[4][1].piece = 107;board[6][1].piece = 108;
		board[1][2].piece = 101;board[3][2].piece = 102;board[5][2].piece = 103;board[7][2].piece = 104;
		
		board[0][5].piece = 201;board[2][5].piece = 202;board[4][5].piece = 203;board[6][5].piece = 204;
		board[1][6].piece = 205;board[3][6].piece = 206;board[5][6].piece = 207;board[7][6].piece = 208;
		board[0][7].piece = 209;board[2][7].piece = 210;board[4][7].piece = 211;board[6][7].piece = 212;	
	
		for (int i=0;i++;i<10) {board[8][i].piece = NIL;board[9][i].piece = NIL;}
		for (int i=0;i++;i<8) {board[i][8].piece = NIL;board[i][9].piece = NIL;}
	} else if (brdSize == 10) {
		board[1][0].piece = 109;board[3][0].piece = 110;board[5][0].piece = 111;board[7][0].piece = 112;board[9][0].piece = 115;
		board[0][1].piece = 105;board[2][1].piece = 106;board[4][1].piece = 107;board[6][1].piece = 108;board[8][1].piece = 114;
		board[1][2].piece = 101;board[3][2].piece = 102;board[5][2].piece = 103;board[7][2].piece = 104;board[9][2].piece = 113;
		
		board[0][7].piece = 201;board[2][7].piece = 202;board[4][7].piece = 203;board[6][7].piece = 204;board[8][7].piece = 213;
		board[1][8].piece = 205;board[3][8].piece = 206;board[5][8].piece = 207;board[7][8].piece = 208;board[9][8].piece = 214;
		board[0][9].piece = 209;board[2][9].piece = 210;board[4][9].piece = 211;board[6][9].piece = 212;board[8][9].piece = 215;
	}
}

void showLight(int x, int y) {
	Form1->light->Left = x-longRect/2;
	Form1->light->Top = y-longRect/2;
	Form1->light->Visible = true;
}

void changeTurn(int *turn) {
	if (*turn == RED) {
		*turn = WHITE;
		Form1->turnlabel->Caption = "PUTIH";
	} else {
		*turn = RED;
		Form1->turnlabel->Caption = "MERAH";
	}
}

bool isWin(clsBoard board[10][10], int *who, int turn, int brdSize) {
	int row,col;
	int red=0;
	int white=0;
	
	// kalau salah satu piece habis
	for (row = 0; row < brdSize; row++) {
		for (col = 0; col < brdSize; col++) {
			if (isRed(board[row][col].piece)) {
				red++;
			} else if (isWhite(board[row][col].piece)) {
				white++;
			}
		}
	}
	if (white==0) {
		*who = RED;
		return true;
	} else if (red==0) {
		*who = WHITE;
		return true;
	} else if (isStuck(turn,board)) { // jika turn sekarang stuck
		if (turn==WHITE) { // WHITE stuck
			*who = RED;
			return true;			
		} else { // RED stuck
			*who = WHITE;
			return true;			
		}
	}
	return false;
}

void convertRow(char hasil[7], int row, int id) {
	switch (row) {
		case (0) : {hasil[id] = 'A'; break;}
		case (1) : {hasil[id] = 'B'; break;}
		case (2) : {hasil[id] = 'C'; break;}
		case (3) : {hasil[id] = 'D'; break;}
		case (4) : {hasil[id] = 'E'; break;}
		case (5) : {hasil[id] = 'F'; break;}
		case (6) : {hasil[id] = 'G'; break;}
		case (7) : {hasil[id] = 'H'; break;}
		case (8) : {hasil[id] = 'I'; break;}
		case (9) : {hasil[id] = 'J'; break;}
	}
}

void convertCol(char hasil[7], int col, int id, int brdSize) {
	if (brdSize == 10 ) {
		switch (col) {
			case (0) : {hasil[id] = '9'; break;}
			case (1) : {hasil[id] = '8'; break;}
			case (2) : {hasil[id] = '7'; break;}
			case (3) : {hasil[id] = '6'; break;}
			case (4) : {hasil[id] = '5'; break;}
			case (5) : {hasil[id] = '4'; break;}
			case (6) : {hasil[id] = '3'; break;}
			case (7) : {hasil[id] = '2'; break;}
			case (8) : {hasil[id] = '1'; break;}
			case (9) : {hasil[id] = '0'; break;}
		}
	} else if (brdSize == 8) {
		switch (col) {
			case (0) : {hasil[id] = '7'; break;}
			case (1) : {hasil[id] = '6'; break;}
			case (2) : {hasil[id] = '5'; break;}
			case (3) : {hasil[id] = '4'; break;}
			case (4) : {hasil[id] = '3'; break;}
			case (5) : {hasil[id] = '2'; break;}
			case (6) : {hasil[id] = '1'; break;}
			case (7) : {hasil[id] = '0'; break;}
		}
	}
}

void showClick(int X, int Y, int _x, int _y) {
	// X,Y = titik awal ; _x,_y = titik akhir;
	int rowAwal, colAwal;
	int rowAkhir, colAkhir;
	char hasil[7];
	
	rowAwal = (_x-xAwal)/longRect; colAwal = (_y-yAwal)/longRect;
	rowAkhir = (X-xAwal)/longRect; colAkhir = (Y-yAwal)/longRect;
	
	convertRow(hasil,rowAwal,0);
	convertCol(hasil,colAwal,1,brdSize);
	hasil[2] = ' ';
	hasil[3] = '-';
	hasil[4] = ' ';
	convertRow(hasil,rowAkhir,5);
	convertCol(hasil,colAkhir,6,brdSize);
	hasil[7] = '\0';
	
	Form1->lastMove->Caption = hasil; 
	Form1->lastMove->Visible = true;
}
//===========================================================================
// select piece
//===========================================================================
 /*white*/
void __fastcall TForm1::whi01MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 101;_x=Form1->whi01->Left+longRect/2;_y=Form1->whi01->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi02MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 102;_x=Form1->whi02->Left+longRect/2;_y=Form1->whi02->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi03MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 103;_x=Form1->whi03->Left+longRect/2;_y=Form1->whi03->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi04MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 104;_x=Form1->whi04->Left+longRect/2;_y=Form1->whi04->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi05MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 105;_x=Form1->whi05->Left+longRect/2;_y=Form1->whi05->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi06MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 106;_x=Form1->whi06->Left+longRect/2;_y=Form1->whi06->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi07MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 107;_x=Form1->whi07->Left+longRect/2;_y=Form1->whi07->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi08MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 108;_x=Form1->whi08->Left+longRect/2;_y=Form1->whi08->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi09MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 109;_x=Form1->whi09->Left+longRect/2;_y=Form1->whi09->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi10MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 110;_x=Form1->whi10->Left+longRect/2;_y=Form1->whi10->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi11MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 111;_x=Form1->whi11->Left+longRect/2;_y=Form1->whi11->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi12MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 112;_x=Form1->whi12->Left+longRect/2;_y=Form1->whi12->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi13MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 113;_x=Form1->whi13->Left+longRect/2;_y=Form1->whi13->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi14MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 114;_x=Form1->whi14->Left+longRect/2;_y=Form1->whi14->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::whi15MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == WHITE && !anotherChance) {id = 115;_x=Form1->whi15->Left+longRect/2;_y=Form1->whi15->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
 /*red*/
void __fastcall TForm1::red01MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 201;_x=Form1->red01->Left+longRect/2;_y=Form1->red01->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red02MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 202;_x=Form1->red02->Left+longRect/2;_y=Form1->red02->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red03MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 203;_x=Form1->red03->Left+longRect/2;_y=Form1->red03->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red04MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 204;_x=Form1->red04->Left+longRect/2;_y=Form1->red04->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red05MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 205;_x=Form1->red05->Left+longRect/2;_y=Form1->red05->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red06MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 206;_x=Form1->red06->Left+longRect/2;_y=Form1->red06->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red07MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 207;_x=Form1->red07->Left+longRect/2;_y=Form1->red07->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red08MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 208;_x=Form1->red08->Left+longRect/2;_y=Form1->red08->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red09MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 209;_x=Form1->red09->Left+longRect/2;_y=Form1->red09->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red10MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 210;_x=Form1->red10->Left+longRect/2;_y=Form1->red10->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red11MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 211;_x=Form1->red11->Left+longRect/2;_y=Form1->red11->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red12MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 212;_x=Form1->red12->Left+longRect/2;_y=Form1->red12->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red13MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 213;_x=Form1->red13->Left+longRect/2;_y=Form1->red13->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red14MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 214;_x=Form1->red14->Left+longRect/2;_y=Form1->red14->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}
void __fastcall TForm1::red15MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (turn == RED && !anotherChance) {id = 215;_x=Form1->red15->Left+longRect/2;_y=Form1->red15->Top+longRect/2;showLight(_x,_y);isKing=board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;}}

//===========================================================================
// posisi piece awal
//===========================================================================
void newGame() {
	sizeDefault(brdSize);
	pictureDefault(brdSize);
	positionDefault(brdSize);
	idDefault(board, brdSize);
	
	turn = RED;
	Form1->turnlabel->Caption = "MERAH";
	id = NIL; _x = NIL; _y = NIL;
	isKing = false; anotherChance = false;
	isPlay = true;
	Form1->light->Visible = false;
	Form1->turnlabel->Visible = true;
}

//===========================================================================
// move the piece
//===========================================================================
void movePiece (int piece, int col, int row) {
   int left = rowColToPixel[col];
   int top  = rowColToPixel[row];
   switch (piece) {
                                                              // White Pieces
      case(101):{Form1->whi01->Left=left;Form1->whi01->Top=top;break;}
      case(102):{Form1->whi02->Left=left;Form1->whi02->Top=top;break;}
      case(103):{Form1->whi03->Left=left;Form1->whi03->Top=top;break;}
      case(104):{Form1->whi04->Left=left;Form1->whi04->Top=top;break;}
      case(105):{Form1->whi05->Left=left;Form1->whi05->Top=top;break;}
      case(106):{Form1->whi06->Left=left;Form1->whi06->Top=top;break;}
      case(107):{Form1->whi07->Left=left;Form1->whi07->Top=top;break;}
      case(108):{Form1->whi08->Left=left;Form1->whi08->Top=top;break;}
      case(109):{Form1->whi09->Left=left;Form1->whi09->Top=top;break;}
      case(110):{Form1->whi10->Left=left;Form1->whi10->Top=top;break;}
      case(111):{Form1->whi11->Left=left;Form1->whi11->Top=top;break;}
      case(112):{Form1->whi12->Left=left;Form1->whi12->Top=top;break;}
      case(113):{Form1->whi13->Left=left;Form1->whi13->Top=top;break;}
      case(114):{Form1->whi14->Left=left;Form1->whi14->Top=top;break;}
      case(115):{Form1->whi15->Left=left;Form1->whi15->Top=top;break;}
                                                              // Red Pieces
      case(201):{Form1->red01->Left=left;Form1->red01->Top=top;break;}
      case(202):{Form1->red02->Left=left;Form1->red02->Top=top;break;}
      case(203):{Form1->red03->Left=left;Form1->red03->Top=top;break;}
      case(204):{Form1->red04->Left=left;Form1->red04->Top=top;break;}
      case(205):{Form1->red05->Left=left;Form1->red05->Top=top;break;}
      case(206):{Form1->red06->Left=left;Form1->red06->Top=top;break;}
      case(207):{Form1->red07->Left=left;Form1->red07->Top=top;break;}
      case(208):{Form1->red08->Left=left;Form1->red08->Top=top;break;}
      case(209):{Form1->red09->Left=left;Form1->red09->Top=top;break;}
      case(210):{Form1->red10->Left=left;Form1->red10->Top=top;break;}
      case(211):{Form1->red11->Left=left;Form1->red11->Top=top;break;}
      case(212):{Form1->red12->Left=left;Form1->red12->Top=top;break;}
      case(213):{Form1->red13->Left=left;Form1->red13->Top=top;break;}
      case(214):{Form1->red14->Left=left;Form1->red14->Top=top;break;}
      case(215):{Form1->red15->Left=left;Form1->red15->Top=top;break;}
   }
}

void __fastcall TForm1::ImageBoardMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
	if (id != NIL && isPlay) {
		int rowTengah, colTengah;
		int idTemp = NIL;
		int who;
		bool hasKill = false;
		rowTengah = (((X+_x)/2)-xAwal)/longRect;
		colTengah = (((Y+_y)/2)-yAwal)/longRect;
		bool range = true;
		row = (X - xAwal) / longRect;
		col = (Y - yAwal) / longRect;
		if (X < xAwal || X > xAkhir || Y < yAwal || Y > yAkhir) {
			range = false;
		}
		// masuk ke kotak di papan
		if (range && (diagonal(X,Y,_x,_y,id)||diagonal2(X,Y,_x,_y,board))) {
			if (diagonal2(X,Y,_x,_y,board) || (diagonal(X,Y,_x,_y,id) && !isChance(turn,board))) {
				if (diagonal2(X,Y,_x,_y,board)) {
					///////////////// bunuh board disini!
					idTemp = board[rowTengah][colTengah].piece; 
					board[rowTengah][colTengah].piece = EMPTY;
					board[rowTengah][colTengah].isKing = false;
					killPiece(idTemp);
					hasKill = true;
					///////////////////////////////////////
				}
				movePiece(id, row, col);
				showClick(X,Y,_x,_y);
				board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].piece = EMPTY;
				board[row][col].piece = id;
				board[row][col].isKing = board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing;
				board[(_x-xAwal)/longRect][(_y-yAwal)/longRect].isKing = false;
				
				// cek jika jadi raja
				if ((isWhite(id) && col==(brdSize-1)) || (isRed(id) && col==0)) {
					board[row][col].isKing = true;
					isKing = true;
					// simpen efek piece jadi raja
					showKing(id);
				}
				// cek jika bisa bunuh lagi
				if (hasKill && isChancePiece(board,row,col)) {
					_x = X; _y = Y;
					showLight(rowColToPixel[row]+longRect/2,rowColToPixel[col]+longRect/2);
					anotherChance = true;
				} else {
					changeTurn(&turn);
					anotherChance = false;
				}
				// cek win
				if (isWin(board,&who,turn,brdSize)) { // jika ada yang menang
					CautionForm->Caption = "Selamat !";
					CautionForm->LabelCaution->Left = 72;
					if (who==RED) {
						CautionForm->LabelCaution->Caption = "Merah Menang !";
						turn = WHITE;
					} else { // who==WHITE
						CautionForm->LabelCaution->Caption = "Putih Menang !";
						turn = RED;
					}
					Form1->light->Visible = false;
					Form1->turnlabel->Visible = false;
					Form1->lastMove->Visible = false;
					CautionForm->ShowModal();
				}
			} else { // ada checker yang bisa di bunuh
				CautionForm->Caption = "Peringatan !";
				CautionForm->LabelCaution->Left = 20;
				CautionForm->LabelCaution->Caption = "Anda harus membunuh checker !";
				CautionForm->ShowModal();
			}			
		}
		if (!anotherChance) {
			id = NIL; _x = NIL; _y = NIL; 
			Form1->light->Visible = false;
		}
	}
}
//===========================================================================
// menu click handle
//===========================================================================
void __fastcall TForm1::NewClick(TObject *Sender) {
	newGame();
}

void __fastcall TForm1::DisplayAbout(TObject *Sender) {
	AboutForm->ShowModal();
}
//---------------------------------------------------------------------------
